package com.wms.wms.service.prouct.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.wms.entity.Product;
import com.wms.wms.entity.Type;
import com.wms.wms.exception.*;
import com.wms.wms.mapper.product.ProductMapper;
import com.wms.wms.mapper.product.TypeMapper;
import com.wms.wms.service.prouct.ProductService;
import com.wms.wms.vo.request.ProductRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Chl
 * @version 1.0
 * @date 2019/12/18 12:22
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductService productService;

    @Override
    public Type selectTypeById(String id) throws NotFoundException {
        Type type = typeMapper.selectByPrimaryKey(id);
        if (type == null) {
            throw new NotFoundException("该类型不存在");
        }
        return type;
    }

    @Override
    public PageInfo<Type> selectTypeByName(String name, Integer start, Integer size) throws NotFoundException {
        Example example = new Example(Type.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("name", "%" + name + "%");
        PageHelper.startPage(start, size);
        List<Type> types = typeMapper.selectByExample(example);
        if (types == null || types.size() == 0) {
            throw new NotFoundException("没有该名字的产品");
        }
        return new PageInfo<>(types);
    }

    @Override
    public PageInfo<Type> selectTypeAll(Integer start, Integer size) throws NotFoundException {
        PageHelper.startPage(start, size);
        List<Type> types = typeMapper.selectAll();
        if (types == null || types.size() == 0) {
            throw new NotFoundException("暂无记录");
        }
        return new PageInfo<>(types);
    }

    @Override
    public void deleteTypeById(String id) throws CannotDeleteException, SystemException {
        int result;
        try {
            result=typeMapper.deleteByPrimaryKey(id);
        } catch (DataAccessException e) {
            throw new CannotDeleteException("库存中还存在当前产品类别，不能删除");
        }
        if (result != 1) {
            throw new SystemException("系统异常");
        }
    }

    @Override
    public void updateType(Type type) throws SystemException {
        int result = typeMapper.updateByPrimaryKeySelective(type);
        if (result != 1) {
            throw new SystemException("系统异常");
        }
    }

    @Override
    public void addType(Type type) throws AlreadyExistsException, SystemException {
        int result;
        try {
            result=typeMapper.insert(type);
        } catch (DataAccessException e) {
            throw new AlreadyExistsException("该编号已存在");
        }
        if (result != 1) {
            throw new SystemException("系统异常");
        }
    }

    @Override
    public PageInfo<Product> selectProductByName(String name, Integer start, Integer size) throws NotFoundException {
        PageHelper.startPage(start,size);
        List<Product> products = productMapper.selectProductByName("%"+name+"%");
        if(products==null||products.size()==0){
            throw new NotFoundException("该名字的产品不存在");
        }
        return new PageInfo<>(products);
    }

    @Override
    public Product selectProductById(String id) throws NotFoundException {
        Product product = productMapper.selectProductById(id);
        if(product==null){
            throw new NotFoundException("没有该编号的产品");
        }
        return product;
    }

    @Override
    public void updateProduct(ProductRequestVO vo) throws SystemException, ErrorTypeIdException {
        Product product = ProductRequestVO.getEntity(vo);
        if(product==null){
            throw new SystemException("系统异常");
        }
        int result;
        try {
            result=productMapper.updateProduct(product);
        } catch (DataAccessException e) {
            throw new ErrorTypeIdException("类别编号错误");
        }
        if (result != 1) {
            throw new SystemException("系统异常");
        }
    }

    @Override
    public void addProduct(ProductRequestVO vo) throws SystemException, ErrorTypeIdException, AlreadyExistsException {
        Product product = ProductRequestVO.getEntity(vo);
        if(product==null){
            throw new SystemException("系统异常");
        }
        int result;
        try {
//            result=productMapper.insert(product);
            result=productMapper.insertProduct(product);
        } catch (DuplicateKeyException e){
            throw new AlreadyExistsException("产品编号已存在");
        }catch (DataAccessException e) {
            throw new ErrorTypeIdException("类别编号错误");
        }
        if (result != 1) {
            throw new SystemException("系统异常");
        }
    }

    @Override
    public void deleteProductById(String id) throws CannotDeleteException, SystemException {
        int result;
        try {
            result=productMapper.deleteByPrimaryKey(id);
        } catch (DataAccessException e) {
            throw new CannotDeleteException("库存中还存在当前产品，不能删除");
        }
        if (result != 1) {
            throw new SystemException("系统异常");
        }
    }

}
